﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using starLiGHT.Collision;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace starLiGHT.Sandbox.Collision
{
    public class CircleObject : ICollidable2D
    {
        public event EventHandler Moved;

        private CircleF boundingCircle;
        private RectangleF boundingRectangle;
        private Boolean worldRectDirty = true;
        private RectangleF worldRect;
        private Transform2D transform;

        public CircleObject(Vector2 position, float radius)
        {
            transform = new Transform2D(position);
            transform.Changed += new EventHandler<EventArgs>(transform_Changed);
            boundingCircle = new CircleF(position, radius);
            float r2 = radius + radius;
            boundingRectangle = new RectangleF(position.X - radius, position.Y - radius, r2, r2);
        }

        void transform_Changed(object sender, EventArgs e)
        {
            worldRectDirty = true;
            if (Moved != null)
            {
                Moved(sender, e);
            }
        }

        public RectangleF LocalBoundingRectangle
        {
            get { return boundingRectangle; }
        }

        public RectangleF WorldBoundingRectangle
        {
            get
            {
                if (worldRectDirty)
                {
                    worldRect = boundingRectangle;
                    worldRect.Center = Vector2.Transform(boundingRectangle.Center, transform.TransformationMatrix);
                    worldRectDirty = false;
                }

                return worldRect;
            }
        }

        public CircleF BoundingCircle
        {
            get { return boundingCircle; }
        }

        public Transform2D Transform
        {
            get { return transform; }
        }

        public Vector2 Position
        {
            get { return boundingCircle.Center; }
        }

        // circle cannot rotate
        public float Orientation
        {
            get { return transform.Orientation; }
        }

        // always the same size for now
        public Vector2 Scale
        {
            get { return transform.Scale.Value; }
        }

        public Texture2D Texture
        {
            get { return null; }
        }

        public bool Colliding
        {
            get;
            set;
        }

        public bool Static
        {
            get;
            set;
        }

        public Hull2D Hull
        {
            get { return null; }
        }

        public Vector2 Velocity
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public object Tag
        {
            get;
            set;
        }

        public void LookAt(Vector2 target)
        {
            // the circle is round so the rotation is not visible ;-)
        }
    }
}
